# -*- coding: ISO-8859-1 -*-
require 'coord'
Place = Struct.new(:point, :char)
SPACE = ' '

class Plot
	attr_accessor :size_x, :size_y, :scale
	
	def initialize(size_x, size_y, scale)
		@size_y = size_y
		@size_x = size_x
		@scale = scale
		@graph = Array.new
		@size_y.times { @graph << SPACE*@size_x }
	end
	
	def clear
		@graph.each_index do |y|
			@graph.each_index do |x|
				@graph[y][x] = SPACE
			end
		end
		
		#@size_y.times { @graph << SPACE*@size_x }
	end
	
	def add(point, char)
		x = (point.x*@scale).to_i
		y = (-point.y*@scale-1).to_i
		if (y >= -@graph.size && y < @graph.size)
			if (x >= -@graph[y].size && x < @graph[y].size) 
			
				#puts "Adding #{point.to_s} as graph[#{y}][#{x}]"  
				@graph[y][x] = char
			end
		end
	end
	
	def plot
		@graph.each { |l| puts l }
	end
	
end

if $0 == __FILE__
	1.upto(4) do |scale|
		scale.upto(20) do |size|
			test = Plot.new(size,size,scale)		
			1.upto(100) do |idx|
				puts "Test #{scale}:#{size}:#{idx}"
				test.clear 
				(size*size+12*size+36).times do
					test.add(Coord.new( (rand(size+6)-3)/scale.to_f, (rand(size+6)-3)/scale.to_f ), (rand(26)+97).chr) if rand < 0.50					
				end
				test.plot
			end
			gets
		end
	end
end
